
namespace AlertMail
{
    using System;
    using System.IO;
    using System.Data;

    interface IBulkEmail
    {
        void SendBulkEmailToUsers();
    }

    public class BulkEmail : IBulkEmail
    {
        string emailTemplateFolder = ""; 
        public BulkEmail(string folderPath)
        {
            emailTemplateFolder = folderPath;
        }

        public void SendBulkEmailToUsers()
        {
            try
            {
                string sentEmailId = "";
                SqlServerProvider sqlServer = new
                SqlServerProvider();
                sqlServer.CommandText =
                "SPGetBulkEmails";
                sqlServer.CommandType =
                CommandType.StoredProcedure;
                DataTable dataTableAlert =
                sqlServer.GetDataTable();
                string toAddress = "";
               
                string subject = "";
                string fullName = "";
                string password = "";
                string content = "";
                int mailType = 0;
                if (dataTableAlert.Rows.Count > 0)
                {
                    for (int i = 0; i < dataTableAlert.Rows.Count; i++)
                    {
                       
                        toAddress = dataTableAlert.Rows[i]["ToAddress"].ToString();
                        subject = dataTableAlert.Rows[i]["Subject"].ToString();
                        mailType = Convert.ToInt32(dataTableAlert.Rows[i]["EmailType"]);
                        fullName = dataTableAlert.Rows[i]["FullName"].ToString();
                        password = dataTableAlert.Rows[i]["Password"].ToString();
                         
                        if (toAddress != "")
                        {                            
                            content = CreateContentForBulkEmail(
                            toAddress, fullName, password, mailType); 
                            if(SendMailToUser(toAddress, 
                               subject, content))
                                sentEmailId += 
                                dataTableAlert.Rows[i][
                                "EmailID"].ToString() + "','";
                        }
                    }
                }
                if(sentEmailId != "")
                {
                    sqlServer.CommandText =
                    "delete from tblEmails where" +
                    " EmailID in ('" + sentEmailId + "')";
                    sqlServer.ExecuteNonQuery();
                }
                sqlServer.CloseConnection();
            }
            catch (Exception ex)
            {
                throw new ApplicationException(
                "BulkEmail::SendEmails", ex);
            }
        }
        bool SendMailToUser( 
        string emailTo, string subject, 
        string contentToSend)
        {
            bool sent = false;
            MailProvider mailProvider = new MailProvider();
            if (mailProvider.IsEmail(emailTo))
                sent = mailProvider.SendMail(
                emailTo, subject, contentToSend);
            return sent;
        }
         
        string CreateContentForBulkEmail(string toAddress,
                        string fullName, string password,
                        int mailType)
        {

            string fileName = "";
            switch (mailType)
            {
                case 0:
                    fileName = "ForgotPassword.txt";
                    break;
                case 1:
                    fileName = "NewJobSeeker.txt";
                    break;
                case 2:
                    fileName = "NewRecruiter.txt";
                    break;
                case 3:
                    fileName = "SendToFriend.txt";
                    break;
            }
            string content = "";
            using (StreamReader sr = new
            StreamReader(emailTemplateFolder 
            + fileName))
            {
                content = sr.ReadToEnd();
            }
            if (toAddress != "")
                content.Replace("[Email]", toAddress);
            if(fullName != "")
                content.Replace("[FullName]", fullName);
            if (password != "") 
                content.Replace("[Password]", password);
            return content;
        }
    }
}
